class Login {
    constructor() {

    }

    async login() {
        //ajax
        const txtUserName = document.getElementById('txtUserName')
        const txtPwd = document.getElementById('txtPwd')
        const resultTxt = document.getElementById('resultTxt')

        const userObj = new User(0, txtUserName.value, txtPwd.value)
        const jstr = JSON.stringify(userObj)

        const result = await fetch('http://localhost:8080/checklogin', {
            method: 'POST', header: {
                'Content-Type': 'application/json'
            }, body: jstr
        })
        const data = await result.text()

        if (data.indexOf('成功') != -1) {
            location.href = 'http://localhost:8080/success.html'
        }
        else {
            location.href = 'http://localhost:8080/error.html'
        }
        //resultTxt.innerText = data

    }
}

class Register {
    async reg() {
        //ajax
        const txtUserName = document.getElementById('txtUserName')
        const txtPwd = document.getElementById('txtPwd')
        const resultTxt = document.getElementById('resultTxt')

        const userObj = new User(0, txtUserName.value, txtPwd.value)
        const jstr = JSON.stringify(userObj)

        const result = await fetch('http://localhost:8080/reg', {
            method: 'POST', header: {
                'Content-Type': 'application/json'
            }, body: jstr
        })
        const data = await result.text()

        if (data.indexOf('成功') != -1) {
            location.href = 'http://localhost:8080/index.html'
        }
        else {
            //location.href = 'http://localhost:8080/reg.html'
            resultTxt.innerText = data
        }
        //resultTxt.innerText = data

    }
}

class User {
    constructor(id, username, pwd) {
        this.id = id
        this.username = username
        this.pwd = pwd
    }
}

class UserList {
    async getuserlist() {
        //ajax

        const userUL = document.getElementById('userUL')

        const result = await fetch('http://localhost:8080/userlist')
        const data = await result.text()


        const jarray = JSON.parse(data)

        let htmlStr = ''
        jarray.forEach(item => {
            htmlStr += `<li id="${'li' + item.id}">${item.username}---<button onclick="Del(${item.id})">删除</button></li>`


        })

        userUL.innerHTML = htmlStr
        //resultTxt.innerText = data

        //resultTxt.innerText = data

    }

}

async function Del(id) {

    const result = await fetch('http://localhost:8080/deluser?id=' + id)

    const liElem = document.getElementById('li' + id)
    liElem.style.display = 'none'
    const resultTxt = document.getElementById('resultTxt')
    const data = await result.text()
    resultTxt.innerText = data
}

function Init(pagename) {

    if (pagename === "login") {
        const logintBtn = document.getElementById('btnLogin')


        const loginObj = new Login()

        logintBtn.addEventListener("click", loginObj.login)
    }
    else if (pagename === "reg") {
        const regBtn = document.getElementById('btnReg')
        const regObj = new Register()
        regBtn.addEventListener("click", regObj.reg)
    }
    else if (pagename === "userlist") {
        const userlistBtn = document.getElementById('btnUserList')
        const userlistObj = new UserList()
        userlistBtn.addEventListener("click", userlistObj.getuserlist)


    }




}

